﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Odbc;

namespace CS3230HealthTrackerSystemGUI
{
    public partial class LoginForm : Form
    {
        //Stores these values to be passed onto children froms
        //so that this information can be put into the database. (Let's use know which user
        //is registering patients, or recording test results.
        String currentUser;
        String userPassword;

        public LoginForm()
        {
            InitializeComponent();
        }

        //Gets the password and username and then goes to the apporpriate file or form.
        private void userLogin_Click(object sender, EventArgs e)
        {
            try
            {
                //Sets the values for the user that is currently logging in.
                currentUser = usernameText.Text;
                userPassword = passwordText.Text;

                //Make sure entries are entered and valid.
                validateEntry(currentUser);
                validateEntry(userPassword);

                //Gets the login in  type from the table so that we can determine if they are logging
                //in as a nurse or an admin.
                String loginType = selectLoginType(currentUser, userPassword);

                if (loginType.Equals("nurse"))
                {
                    nurseLogin();
                }

                else if (loginType.Equals("admin"))
                {
                    adminLogin();
                }

                else
                {
                    MessageBox.Show("You have entered an invalid Username or Password, please try again.");
                }
            }

            catch (Exception formatException)
            {
                MessageBox.Show(formatException.Message);
            }
        }

        private void validateEntry(String entry)
        {
            String currentName = entry.Replace(" ", "");
            Char[] nameTester = currentName.ToCharArray();

            if (nameTester.Length <= 0)
            {
                throw new ArgumentException("Must enter a username or password.");
            }

        }

        private String selectLoginType(string userName, string password)
        {
            String login = "";
            int loginId = Int32.Parse(userName);
            int value = 0;
            
                string MyConString = "DRIVER={MySQL ODBC 5.1 Driver};" +
                     "SERVER=cs.westga.edu;" +
                     "PORT=3307;" +
                     "DATABASE=ccole5;" +
                     "UID=ccole5;" +
                     "PASSWORD=cc@6070";

                OdbcConnection MyConnection = new OdbcConnection(MyConString);

                MyConnection.Open();

                
                OdbcCommand MyCommand = new OdbcCommand("SELECT COUNT(*) FROM `LOGIN`,`NURSE` WHERE LOGIN.loginID=" + loginId + " AND LOGIN.loginID=NURSE.nurseID AND LOGIN.password='" + password + "';", MyConnection);
                OdbcDataReader MyDataReader;
                MyDataReader = MyCommand.ExecuteReader();

                while (MyDataReader.Read())
                {
                    login = MyDataReader.GetString(0);
                    value = Int32.Parse(login);
                }

                if (value == 1)
                {
                    login = "nurse";
                }

                else if (value == 0)
                {
                    MyCommand = new OdbcCommand("SELECT COUNT(*) FROM `LOGIN`,`ADMINISTRATOR` WHERE LOGIN.loginID=" + loginId + " AND LOGIN.loginID=ADMINISTRATOR.adminID AND LOGIN.password='" + password + "';", MyConnection);
                    MyDataReader = MyCommand.ExecuteReader();

                    while (MyDataReader.Read())
                    {
                        login = MyDataReader.GetString(0);
                        value = Int32.Parse(login);
                    }

                    if (value == 1)
                    {
                        login = "admin";
                    }
                }


                //Close all resources
                MyDataReader.Close();
                MyConnection.Close();
          
                return login;
                  
        }
        
        private void nurseLogin()
        {
            //Opens up the appropriate nurse form.
            NurseMenu nurseMenu = new NurseMenu();
            //Makes the current form close the parent form when it is closed.
            nurseMenu.FormClosed += new FormClosedEventHandler(reopenForm);
            nurseMenu.Show();
            //Hides the parent.
            this.Hide();
            //Should close login menu
        }

        private void adminLogin()
        {
            //Opens up the appropriate administrator form
            AdministratorMenu adminMenu = new AdministratorMenu();
            //Makes the current form close the parent form when it is closed.
            adminMenu.FormClosed += new FormClosedEventHandler(reopenForm);
            adminMenu.Show();
            //Hides the parent.
            this.Hide();
            //Should close login menu
        }

        private void reopenForm(object sender, FormClosedEventArgs e)
        {
            usernameText.Clear();
            passwordText.Clear();
            this.Show();
        }

     
      
    }
}
